<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="60%">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-descriptions title="基础信息" :column="1">
        <el-descriptions-item>
          <el-form-item label="姓名" prop="name">
            <el-input v-model="formData.name" placeholder="请输入姓名" />
          </el-form-item>
          <el-form-item label="性别" prop="gender">
            <el-radio-group v-model="formData.gender">
              <el-radio
                v-for="dict in getStrDictOptions(DICT_TYPE.SYSTEM_USER_SEX)"
                :key="dict.value"
                :label="dict.value"
              >
                {{ dict.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="出生日期" prop="birthday">
            <el-date-picker
              type="date"
              v-model="formData.birthday"
              placeholder="请输入出生日期"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
          <el-form-item label="联系电话" prop="phone">
            <el-input v-model="formData.phone" placeholder="请输入联系电话" />
          </el-form-item>
          <el-form-item label="电子邮箱" prop="email">
            <el-input v-model="formData.email" placeholder="请输入电子邮箱" />
          </el-form-item>
          <el-form-item label="地址" prop="address">
            <el-input v-model="formData.address" placeholder="请输入地址" />
          </el-form-item>
          <el-form-item label="学校" prop="school">
            <el-input v-model="formData.school" placeholder="请输入学校" />
          </el-form-item>
          <el-form-item label="专业" prop="major">
            <el-input v-model="formData.major" placeholder="请输入专业" />
          </el-form-item>
          <el-form-item label="毕业年份" prop="graduationDate">
            <el-date-picker
              v-model="formData.graduationDate"
              type="year"
              placeholder="选择毕业时间"
              format="YYYY"
              value-format="YYYY"
            />
          </el-form-item>
          <el-form-item label="求职意向" prop="jobObjective">
            <el-select
              v-model="formData.jobObjective"
              multiple
              filterable
              placeholder="请选择求职意向"
            >
              <el-option
                v-for="item in jobTitleList"
                :key="item.jobObjective"
                :label="item.jobTitle"
                :value="item.jobTitle"
              />
            </el-select>
          </el-form-item>
        </el-descriptions-item>
      </el-descriptions>
      <el-descriptions title="项目经历" :column="1">
        <el-descriptions-item>
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="formData.projectName" placeholder="请输入项目名称" />
          </el-form-item>
          <el-form-item label="项目起始时间" prop="projectStartDate">
            <el-date-picker
              v-model="formData.projectStartDate"
              type="date"
              placeholder="选择项目起始时间"
               format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
          <el-form-item label="项目结束时间" prop="projectEndDate">
            <el-date-picker
              v-model="formData.projectEndDate"
              type="date"
              placeholder="选择项目结束时间"
               format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
          <el-form-item label="项目介绍" prop="projectDescription">
            <el-input
              type="textarea"
              maxlength="200"
              show-word-limit
              v-model="formData.projectDescription"
              height="150px"
            />
          </el-form-item>
          <el-form-item label="个人职责" prop="personalResponsibilities">
            <el-input
              type="textarea"
              maxlength="200"
              show-word-limit
              v-model="formData.personalResponsibilities"
              placeholder="请输入个人职责"
            />
          </el-form-item>
        </el-descriptions-item>
      </el-descriptions>
      <el-descriptions title="其他信息" :column="1">
        <el-descriptions-item>
          <el-form-item label="技能证书" prop="skillsAndCertificates">
            <el-input
              type="textarea"
              maxlength="100"
              show-word-limit
              v-model="formData.skillsAndCertificates"
              placeholder="请输入技能证书"
            />
          </el-form-item>
          <el-form-item label="自我评价" prop="selfEvaluation">
            <el-input
              type="textarea"
              maxlength="100"
              show-word-limit
              v-model="formData.selfEvaluation"
              placeholder="请输入自我评价"
            />
          </el-form-item>
          <el-form-item label="兴趣爱好" prop="interestsAndHobbies">
            <el-input
              type="textarea"
              maxlength="100"
              show-word-limit
              v-model="formData.interestsAndHobbies"
              placeholder="请输入兴趣爱好"
            />
          </el-form-item>
          <el-form-item label="其他信息" prop="otherInformation">
            <el-input
              type="textarea"
              v-model="formData.otherInformation"
              placeholder="如实习经历、社团活动等"
              maxlength="100"
              show-word-limit
            />
          </el-form-item>
        </el-descriptions-item>
      </el-descriptions>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { OnlineResumesInfoApi, OnlineResumesInfoVO } from '@/api/dbit/onlineresumesinfo'
import { dateFormatter } from '@/utils/formatTime'
import { JobTitlesApi } from '@/api/dbit/jobtitles'
/** 在线简历信息 表单 */
defineOptions({ name: 'OnlineResumesInfoForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const jobTitleList = ref([]) // 职位列表
const formData = ref({
  name: undefined,
  gender: undefined,
  birthday: undefined,
  phone: undefined,
  email: undefined,
  address: undefined,
  school: undefined,
  major: undefined,
  graduationDate: undefined,
  jobObjective: undefined,
  projectName: undefined,
  projectStartDate: undefined,
  projectEndDate: undefined,
  projectDescription: undefined,
  personalResponsibilities: undefined,
  skillsAndCertificates: undefined,
  selfEvaluation: undefined,
  interestsAndHobbies: undefined,
  languageAbility: undefined,
  otherInformation: undefined
})
const formRules = reactive({
  name: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
  gender: [{ required: true, message: '性别不能为空', trigger: 'blur' }],
  birthday: [{ required: true, message: '出生日期不能为空', trigger: 'blur' }],
  phone: [{ required: true, message: '联系电话不能为空', trigger: 'blur' }],
  email: [{ required: true, message: '电子邮箱不能为空', trigger: 'blur' }],
  school: [{ required: true, message: '学校不能为空', trigger: 'blur' }],
  major: [{ required: true, message: '专业不能为空', trigger: 'blur' }],
  graduationDate: [{ required: true, message: '毕业时间(届)不能为空', trigger: 'blur' }],
  jobObjective: [{ required: true, message: '求职意向不能为空', trigger: 'change' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  JobTitleList()
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await OnlineResumesInfoApi.getOnlineResumesInfo(id)
      if (formData.value.jobObjective != undefined) {
        formData.value.jobObjective = formData.value.jobObjective.split(';')
      }
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    if (formData.value.jobObjective != undefined) {
      formData.value.jobObjective = formData.value.jobObjective.join(';')
    }
    const data = formData.value as unknown as OnlineResumesInfoVO
    if (formType.value === 'create') {
      await OnlineResumesInfoApi.createOnlineResumesInfo(data)
      message.success(t('common.createSuccess'))
    } else {
      await OnlineResumesInfoApi.updateOnlineResumesInfo(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 获取求职岗位列表 */
const JobTitleList = async () => {
  const data = await JobTitlesApi.getJobTitlesList()
  jobTitleList.value = data
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    name: undefined,
    gender: undefined,
    birthday: undefined,
    phone: undefined,
    email: undefined,
    address: undefined,
    school: undefined,
    major: undefined,
    graduationDate: undefined,
    jobObjective: undefined,
    projectName: undefined,
    projectStartDate: undefined,
    projectEndDate: undefined,
    projectDescription: undefined,
    personalResponsibilities: undefined,
    skillsAndCertificates: undefined,
    selfEvaluation: undefined,
    interestsAndHobbies: undefined,
    languageAbility: undefined,
    otherInformation: undefined
  }
  formRef.value?.resetFields()
}
</script>
